package com.tehui.activity.impl;

import android.content.Intent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.cache.CacheMode;
import com.lzy.okgo.callback.StringCallback;
import com.tehui.R;
import com.tehui.activity.BaseActivity;
import com.tehui.bean.NewsData;
import com.tehui.utils.ui.UiUtils;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.List;

import butterknife.BindView;
import okhttp3.Call;
import okhttp3.Response;

public class MessageActivity extends BaseActivity {

    @BindView(R.id.lv)
    ListView lv;
    @BindView(R.id.empty)
    TextView empty;
    private List<NewsData.NewsBean> news;
    private String                  taokeId;

    @Override
    public void initActivityData() {
        super.initActivityData();
        taokeId = getString(R.string.taokeId);


        if (!UiUtils.isNetworkAvailable()) {
            lv.setVisibility(View.GONE);
            empty.setVisibility(View.VISIBLE);
        } else {
            getServre();
        }

        tvTitle.setText("我的消息");

        final Intent intent = new Intent(this, WebActivity.class);
        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                String message = news.get(position).message;
                intent.putExtra("url", message);
                jumpToActivity(intent);
            }
        });
    }

    @Override
    protected int provideContentViewId() {
        return R.layout.activity_message;
    }

    public void getServre() {
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("TaokeId", taokeId);
            String s = jsonObject.toString();
            OkGo.get("http://dl.21tehui.com/AppNewsApi.html?user=" + s).tag(this).cacheKey("cacheKey")
                    .cacheMode(CacheMode.DEFAULT).execute(new StringCallback() {
                @Override
                public void onSuccess(String s, Call call, Response response) {
                    if (s != null) {
                        parseJson(s);
                    }
                }

                @Override
                public void onError(Call call, Response response, Exception e) {
                }
            });

        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    //解析
    private void parseJson(String s) {

        NewsData newsData = JSON.parseObject(s, NewsData.class);
        news = newsData.news;

        if (news.size() == 0) {
            lv.setVisibility(View.GONE);
            empty.setVisibility(View.VISIBLE);
        } else {
            lv.setVisibility(View.VISIBLE);
            lv.setAdapter(new MyAdapter());
        }
    }

    class MyAdapter extends BaseAdapter {

        @Override
        public int getCount() {
            return news.size();
        }

        @Override
        public Object getItem(int position) {
            return null;
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder;
            if (convertView == null) {
                convertView = View.inflate(UiUtils.getContext(), R.layout.item_message_layout, null);
                holder = new ViewHolder();
                holder.textView = (TextView) convertView.findViewById(R.id.tvTitle);
                holder.time = (TextView) convertView.findViewById(R.id.time);
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }

            holder.textView.setText(news.get(position).title);
            holder.time.setText(news.get(position).time);

            return convertView;
        }
    }

    static class ViewHolder {
        TextView textView;
        TextView time;
    }
}
